﻿using MaterialDesignThemes.Wpf;
using Prism.Commands;
using StarryEdge.Core.Enums;
using StarryEdge.Core.Models;
using StarryEdge.Core.Parameter;
using StarryEdge.Menu.User.Models;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarryEdge.Menu.User.ViewModels.Dialog
{
    public class EditRolePopupDialog
    {

        public ObservableCollection<RoleModel> Roles { get; set; }
        private IUIRoleManage _roleManage;
        private UIRoleItem _roleItem;

        public EditRolePopupDialog(IUserEntity userEntity, IUIRoleManage roleManage, UIRoleItem roleItem)
        {
            Roles = new ObservableCollection<RoleModel>(userEntity.GetRoles());
            _roleManage = roleManage;
            _roleItem = roleItem;
        }

        public DelegateCommand<RoleModel> Save => new DelegateCommand<RoleModel>(sender =>
        {
            if (sender != null)
            {
                var role = _roleManage.GetUIRoleModel(x => x.Level == _roleItem.Level && x.Title == _roleItem.Title && _roleItem.Name == x.Name);
                if (role != null && !role.GuidRoles.Any(x => x.Id == sender.Id))
                {
                    var model = new UIRoleItemModel()
                    {
                        Id = sender.Id,
                        Name = sender.Name
                    };
                    role.GuidRoles.Add(model);
                    _roleItem.GuidRoles.Add(model);
                    _roleManage.Save();
                    DialogHost.Close(PopUpTypes.Global.ToString());
                }
            }
        });
    }
}
